/**
 * Created by Jiao Zhongxiao on 2016/7/18.
 */
package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;

	public class HolePoint
	{
		public static const APPEAR_START:String = "appear_start";
		public static const APPEAR_END:String = "appear_end";
		public static const DISAPPEAR_START:String = "disappear_start";
		public static const DISAPPEAR_START_NORMAL:String = "disappear_start_normal";
		public static const DISAPPEAR_END:String = "disappear_end";

		public function HolePoint( skin:MovieClip, infoTF:TextField )
		{
			_infoTF = infoTF;
			_skin = skin;

			_oriPos.setTo( _skin.x, _skin.y );

			_skin.mouseChildren = false;
			_skin.addEventListener( MouseEvent.CLICK, onMouseClickHandler );

			_ani = new DragonAni();

			_skin.addChild( _ani );
			_ani.stop();
			_ani.visible = false;
			
			_ani.addEventListener( APPEAR_END, onAppearEndHandler );
			_ani.addEventListener( DISAPPEAR_END, onDisappearEndHandler );
		}
		
		private function onDisappearEndHandler(event:Event):void
		{
			_ani.stop();
			_ani.visible = false;
			_refreshable = true;
			curTotal--;
		}
		
		private function onAppearEndHandler(event:Event):void
		{
			_clickable = true;
			_skin.buttonMode = true;
			_disappearTimer = disappearTime;
		}

		private function onMouseClickHandler(event:MouseEvent):void
		{
			_skin.buttonMode = false;
			if ( _clickable )
			{
//				curTotal--;
				_clickable = false;

				if ( event )
				{
					killTotal++;
					updateInfo();
					_ani.gotoAndPlay( DISAPPEAR_START );
				}
				else
				{
					_ani.gotoAndPlay( DISAPPEAR_START_NORMAL );
				}
			}
		}

		public function update():void
		{
			checkIsNeedDisappear();

			checkIsNeedAppear();

			if ( _clickable )
			{
				_skin.parent.addChild( _skin );
			}
		}

		private function checkIsNeedAppear():void
		{
			if ( curTotal < curMax && Math.random() > 0.75 && _refreshable )
			{
				_ani.gotoAndPlay( APPEAR_START );
				_ani.visible = true;
				_refreshable = false;

				_skin.x = _oriPos.x + Math.random() * 100 - 50;
				_skin.y = _oriPos.y - Math.random() * 50;

				curTotal++;
				appearTotal++;

				updateInfo();
//				if ( appearTotal > 100 )
//				{
////					curMax = 5;
//					disappearTime = 30 * 0.5;
//				}
//				else if ( appearTotal > 50 )
//				{
////					curMax = 4;
//					disappearTime = 30 * 1;
//				}
//				else if ( appearTotal > 10 )
//				{
////					curMax = 3;
//					disappearTime = 30 * 3;
//				}
			}
		}

		private function updateInfo():void
		{
			_infoTF.text = killTotal + "/" + appearTotal;
		}

		private function checkIsNeedDisappear():void
		{
			if ( _disappearTimer > 0 )
			{
				_disappearTimer--;

				if ( _disappearTimer <= 0 )
				{
					onMouseClickHandler( null );
				}
			}
		}

		public function dispose():void
		{
			_ani.removeEventListener( APPEAR_END, onAppearEndHandler );
			_ani.removeEventListener( DISAPPEAR_END, onDisappearEndHandler );
			_ani.stop();
			_skin.removeChild( _ani );

			_infoTF = null;

			_skin.removeEventListener( MouseEvent.CLICK, onMouseClickHandler );
		}

		private var _ani:DragonAni;

		private var _skin:MovieClip;

		private var _infoTF:TextField;

		private var _clickable:Boolean = false;

		private var _refreshable:Boolean = true;

		/**
		 * 当前出现的总数
		 */
		private static var curTotal:int = 0;

		private static var killTotal:int = 0;

		/**
		 * 总共出现的总数
		 */
		private static var appearTotal:int = 0;

		private static var curMax:int = 3;

		private static var disappearTime:int = 30 * 5;

		private var _disappearTimer:int;

		private var _oriPos:Point = new Point();
	}
}
